package com.baseweb.webadmin.controller.app;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.model.bvo.ByUserExampleBvo;
import com.baseweb.webadmin.core.model.bvo.ByUserRecommendBvo;
import com.baseweb.webadmin.core.model.bvo.UserInfoBvo;
import com.baseweb.webadmin.core.model.pojo.*;
import com.baseweb.webadmin.core.util.RequestPage;
import com.baseweb.webadmin.core.util.ResultPage;
import com.baseweb.webadmin.service.RoleService;
import com.baseweb.webadmin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by zhouxin on 2017/8/22.
 */
@Controller
@RequestMapping(value = "/appApiExample")
public class ExampleAppController extends BaseController {
    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @ResponseBody
    @RequestMapping(value = "/getMyExamplePageList", method = RequestMethod.POST)
    public Map<String, Object> getMyExamplePageList(RequestPage requestPage, ByUserExampleBvo exampleBvo){
        exampleBvo = exampleBvo == null ? new ByUserExampleBvo() : exampleBvo;
        exampleBvo.setExampleType(3);
        List<ByUserExampleBvo> list = userService.selectExampleBvoListByPage(exampleBvo,requestPage);
        ResultPage examplePage = new ResultPage<>(requestPage.getTotal(),requestPage.getPageSize(),requestPage.getCurPage(),list);
        return success(examplePage,"查询成功");
    }

    @ResponseBody
    @RequestMapping(value = "/getExamplePageList", method = RequestMethod.POST)
    public Map<String, Object> getExamplePageList(RequestPage requestPage, ByUserExampleBvo exampleBvo){
        exampleBvo = exampleBvo == null ? new ByUserExampleBvo() : exampleBvo;
        UserInfoBvo userInfoBvo = exampleBvo.getUserInfoBvo() == null ? new UserInfoBvo() : exampleBvo.getUserInfoBvo();
        Integer userId = userInfoBvo.getId();
        userInfoBvo.setId(null);
        exampleBvo.setDescn("case");
        exampleBvo.setUserInfoBvo(userInfoBvo);
        List<ByUserExampleBvo> list = userService.selectExampleBvoListByPage(exampleBvo,requestPage);
        if(list != null && null != userId){
            for(ByUserExampleBvo example : list){
                if(userService.findUserFollowByUserFollowUserId(userId,example.getId())!=null){
                    example.setFollow(true);
                }
            }
        }
        ResultPage examplePage = new ResultPage<>(requestPage.getTotal(),requestPage.getPageSize(),requestPage.getCurPage(),list);
        return success(examplePage,"查询成功");
    }

    /**
     * 设置作品状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping(value = "/updateExampleView", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> passExampleView(@RequestParam(value = "id",required = true) Integer id,
                                  @RequestParam(value = "status",defaultValue = "1") Integer status) {

        ByUserExample byUserExample = userService.findExample(id);
        if(status != null)
            byUserExample.setStatus(status);

        int num = userService.insertExample(byUserExample);

        if(num > 0){
            return success(byUserExample,"设置成功");
        }else{
            return fail("设置失败");
        }
    }

    /**
     * 查询字典表
     * @param type  1:类型 2： 环境
     * @return
     */
    @RequestMapping(value = "/getDictList")
    @ResponseBody
    public Map<String, Object> getDictList(@RequestParam(value = "type", required = true) Integer type) {
        RequestPage requestPage = new RequestPage();
        requestPage.setCurPage(1);
        requestPage.setPageSize(10000);
        List<Dict> list = userService.selectDictListByPage(type,requestPage);

        return success(list,"设置成功");
    }

    /**
     * 分页查询婚礼预算
     * @param requestPage
     * @param example_id
     * @return
     */
    @RequestMapping(value = "/getExampleBudgetListByPage")
    @ResponseBody
    public Map<String, Object> getExampleBudgetListByPage(RequestPage requestPage,
                                                                  @RequestParam(value = "exampleId", required = true) Integer example_id) {

        List<ByExampleBudget> list = userService.selectExampleBudgetListByPage(example_id,requestPage);

        ResultPage budget = new ResultPage<>(requestPage.getTotal(),requestPage.getPageSize(),requestPage.getCurPage(),list);

        return success(budget,"婚礼明细查询成功");
    }


    /**
     * 案例中详情查询
     * @param exampleBvo
     * @return
     */
    @RequestMapping(value = "/getExampleInfo")
    @ResponseBody
    public Map<String, Object> getExampleInfo(ByUserExampleBvo exampleBvo) {
        RequestPage requestPage = new RequestPage();
        List<ByUserExampleBvo> list = userService.selectExampleBvoListByPage(exampleBvo,requestPage);
        exampleBvo = list != null && list.size() > 0 ? list.get(0) : new ByUserExampleBvo();
        int userId = exampleBvo.getUserInfoBvo() == null || exampleBvo.getUserInfoBvo().getId() == null ? 0 : exampleBvo.getUserInfoBvo().getId();
        Role role = roleService.findRoleByUserIdType(userId,"4");
        exampleBvo.setDescn(role == null ? null : role.getRemark());
        List<ByUserBizAuth> authTypeList = userService.findUserBizAuthByUserId(userId);
        String authType = "";
        if(authTypeList != null){
            for(ByUserBizAuth auth : authTypeList){
                authType += auth.getAuthType() + ",";
            }
        }
        exampleBvo.setOrderDesn(authType);
        return success(exampleBvo,"商家详情查询成功");
    }

    /**
     * 分页查询作品列表下的图片
     * @param requestPage
     * @param example_id
     * @return
     */
    @RequestMapping(value = "/getMyExamplePicList")
    @ResponseBody
    public Map<String, Object> getMyExamplePicList(RequestPage requestPage,
                                                                @RequestParam(value = "id", required = false) Integer example_id) {

        List<ByUserExampleDetail> listPic = userService.selectExampleDetailListByPage(null, example_id, null, requestPage.getStart(), requestPage.getPageSize());
        int total = userService.selectExampleDetailListCount(null,example_id,null);
        ResultPage picPage =  new ResultPage<>(total,requestPage.getPageSize(),requestPage.getCurPage(),listPic);
        return success(picPage,"案例图片查询成功");
    }

    /**
     * 推荐分页查询
     * @param example_id
     * @return
     */
    @RequestMapping(value = "/getRecommendUserList")
    @ResponseBody
    public Map<String, Object> getRecommendUserList(@RequestParam(value = "id", required = false) Integer example_id) {

        RequestPage requestPage = new RequestPage();
        requestPage.setPageSize(10000);
        List<ByUserRecommendBvo> list = userService.selectByUserRecommendBvoListByPage(null,null,example_id,requestPage);
        return success(list,"案例推荐查询成功");
    }

    /**
     * 分页查询案例评论
     * @param requestPage
     * @param example_id
     * @return
     */
    @RequestMapping(value = "/getExampleDiscussListByPage")
    @ResponseBody
    public Map<String, Object> getExampleDiscussListByPage(RequestPage requestPage,
                                                                    @RequestParam(value = "id", required = false) Integer example_id) {
        List<ByExampleDiscuss> list = userService.selectExampleDiscussListByPage(example_id,requestPage);
        ResultPage resultPage = new ResultPage<>(requestPage.getTotal(),requestPage.getPageSize(),requestPage.getCurPage(),list);
        return success(resultPage,"案例评论查询成功");
    }

}
